<%--
  Created by IntelliJ IDEA.
  User: asus
  Date: 2019/7/29
  Time: 16:26
  To change this template use File | Settings | File Templates.

  测试请把spring-web.xml 这一行的注释去除
  <mvc:resources location="WEB-INF/js/" mapping="/js/**"/>
--%>


<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
    <title>订单列表</title>
<%--    <script type="text/javascript"  src="/js/jquery.min.js"></script>--%>
    <script type="text/javascript"  src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
    <script>
        $(function(){
            $("#pay").click(function(){

                var selectedData =[]
                $(":checkbox:checked").each(function(){
                    //获取该节点最近的一个tr
                    var tr = $(this).closest("tr");

                    var id = $(tr).find("td").eq(1).text();
                    var goodsId= $(tr).find("td").eq(2).text();
                    var sellerName= $(tr).find("td").eq(3).text();
                    var buyerName= $(tr).find("td").eq(4).text();
                    var status= $(tr).find("td").eq(5).text();
                    var goodsQty= parseInt($(tr).find("td").eq(6).text());
                    var price= $(tr).find("td").eq(7).text();
                    var createDate= $(tr).find("td").eq(8).text();
                    var sellerAddr= $(tr).find("td").eq(9).text();
                    var buyerAddr= $(tr).find("td").eq(10).text();
                    //将数据装成对象数组
                    // selectedData.push({orders:{orderItem:{id:id,goodsId:goodsId,sellerName:sellerName,buyerName:buyerName,status:status,goodsQty:goodsQty,
                    //             createDate:createDate,sellerAddr:sellerAddr,buyerAddr:buyerAddr},price:price}});


                    //订单
                    var orderItem ={
                        id:id,
                        goodsId:goodsId,
                        sellerName:sellerName,
                        buyerName:buyerName,
                        status:status,
                        goodsQty:goodsQty,
                        createDate:createDate,
                        sellerAddr:sellerAddr,
                        buyerAddr:buyerAddr
                    };
                    //封装成order
                    var temp = {
                        orderItem:orderItem,
                        price:price
                    };
                    //封装进payform里面
                    selectedData.push(temp);
                });
                var json = {
                    orders:selectedData,
                    wallet:{
                        payId:"1",
                        username:"113",
                        password:"123456"
                    }
                };
               console.log(json);

                $.ajax({
                    url:"${pageContext.request.contextPath}/wallet/pay",
                    method:"POST",
                    dataType:"json",
                    contentType : "application/json;charset=UTF-8",
                    <!-- 向后端传输的数据 -->
                    // data : JSON.stringify(selectedData),
                    data : JSON.stringify(json),
                    //json格式应该如下

                    // date:JSON.stringify({
                    //     "orders":
                    //         [
                    //             {
                    //                 "orderItem":
                    //                     {
                    //                         "id":"1",
                    //                         "goodsId":"1",
                    //                         "sellerName":"417",
                    //                         "buyerName":"113",
                    //                         "status":1,
                    //                         "goodsQty":100,
                    //                         "createDate":"2019-07-30 12:59:39",
                    //                         "sellerAddr":"华农",
                    //                         "buyerAddr":"华农"
                    //                     },
                    //                 "price":10.0
                    //             },
                    //             {
                    //                 "orderItem":{"id":"2","goodsId":"c0328de2a47e4e069abc61f7f2ad949c","sellerName":"417","buyerName":"113","status":1,"goodsQty":2,"createDate":"2019-07-30 12:59:39","sellerAddr":"华农","buyerAddr":"华农"},"price":10.0
                    //             }
                    //         ],
                    //     "wallet":{"payId":null,"password":"123","balance":null,"username":"113"}
                    //     }),
                    success:function(result) {
                        <!-- 处理后端返回的数据 -->
                        var message = eval(result);
                        // $("#id").val(message.name);
                        // $("#password").val(message.password);
                        // console.log("success")
                    },
                    error:function(result){
                        $("#select-box").html("查询失败");
                        console.log("failed: " + result)
                    }

                });
            });
        });
    </script>

</head>
<body>
    <div id="data">
        <table align='center' border='1' cellspacing='0'>
            <tr>
                <td></td>
                <td>id</td>
                <td>goodsId</td>
                <td>sellerName</td>
                <td>buyerName</td>
                <td>status</td>
                <td>goodsQty</td>
                <td>price</td>
                <td>createDate</td>
                <td>sellerAddr</td>
                <td>buyerAddr</td>
                <td>编辑</td>
                <td>删除</td>

            </tr>
            <c:forEach items="${orders}"  varStatus="st"  var="c">
                <tr>
                    <td><input type="checkbox" name="check"  value="${c.orderItem.id}"/>${ st.index + 1}</td>
                    <td>${c.orderItem.id}</td>
                    <td>${c.orderItem.goodsId}</td>
                    <td>${c.orderItem.sellerName}</td>
                    <td>${c.orderItem.buyerName}</td>
                    <td>${c.orderItem.status}</td>
                    <td>${c.orderItem.goodsQty}</td>
                    <td>${c.price}</td>
                    <td>${c.orderItem.createDate}</td>
                    <td>${c.orderItem.sellerAddr}</td>
                    <td>${c.orderItem.buyerAddr}</td>
                    <td><a href="categories/${c.orderItem.id}">编辑</a></td>
                    <td><a class="delete" href="categories/${c.orderItem.id}">删除</a></td>
                </tr>
            </c:forEach>
            <button id="pay" type="button"> 支付 </button>
        </table>

    </div>

</body>
</html>
